package com.ruoyi.project.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.service.ShareService;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Locale;

/**
 * @author abel lee
 * asdtiangxia@163.com tel:18190910296   qq:250753635
 * @create 2023-09-23 16:21
 **/
@Service
public class ShareServiceImpl implements ShareService, ApplicationContextAware {

    private static final Logger logger = LoggerFactory.getLogger(ShareServiceImpl.class);
    private static ApplicationContext applicationContext;
    private static String ENV = "";

    @Resource
    private ISysConfigService sysConfigService;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        ShareServiceImpl.applicationContext = applicationContext;
        String[] profiles = applicationContext.getEnvironment().getActiveProfiles();
        ENV = org.apache.commons.lang3.StringUtils.join(profiles, ",");
        logger.info("env--->{}", ENV);
        logger.info("dev mode--->{}", devEnv());
    }

    @Override
    public boolean devEnv() {
        return ENV.contains("dev,") || ENV.equals("dev") || ENV.endsWith(",dev");
    }

    @Override
    public boolean testEnv() {
        return ENV.contains("test,") || ENV.equals("test") || ENV.endsWith(",test");
    }


    @Override
    public boolean isMobile(HttpServletRequest request) {
        boolean isMobile = false;
        String[] mobileAgents = {"iphone", "android", "phone", "mobile", "wap", "netfront", "java", "operamobi", "operamini", "ucweb", "windowsce", "symbian", "series", "webos", "sony", "blackberry", "dopod", "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma", "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", "pantech", "gionee", "portalmmm", "jigbrowser", "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220", "w3c", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-", "Googlebot-Mobile"};
        if (request.getHeader("User-Agent") != null) {
            for (String mobileAgent : mobileAgents) {
                if (request.getHeader("User-Agent").toLowerCase().contains(mobileAgent)) {
                    isMobile = true;
                    break;
                }
            }
        }
        return isMobile;
    }

}
